package main

import (
	"bufio"
	"fmt"
	"strings"
)

func test1() {
	comment := "this is a long long text,because i will use it to test something ..."
	basicReader := strings.NewReader(comment)
	fmt.Println(basicReader.Size()) //68

	// 示例1: 此时reader1的缓冲区还没有被填充
	reader1 := bufio.NewReader(basicReader)
	fmt.Println(reader1.Size(), reader1.Buffered()) //4096 0

	// 示例2: Peek方法 读取并返回其缓冲区中的n个未读字节
	bytes, err := reader1.Peek(7)
	if err != nil {
		fmt.Printf("error: %v\n", err)
	}
	fmt.Println(len(bytes), bytes, string(bytes))   //7 [116 104 105 115 32 105 115] this is
	fmt.Println(reader1.Size(), reader1.Buffered()) //4096 68

	// 示例3: Read方法 把缓冲区中的未读字节，依次拷贝到其参数p代表的字节切片中
	buf1 := make([]byte, 7)
	n, err := reader1.Read(buf1)
	if err != nil {
		fmt.Printf("error: %v\n", err)
	}
	fmt.Println(n, buf1, string(buf1))              //7 [116 104 105 115 32 105 115] this is
	fmt.Println(reader1.Size(), reader1.Buffered()) //4096 61
}

func main() {
	test1()
}
